package com.example.ui_view.mediaplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;

import com.example.ui_view.R;

public class MediaPlayerActivity extends AppCompatActivity {
    MediaPlayer mediaPlayer;
    boolean isPlaying = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_player);
        mediaPlayer = MediaPlayer.create(this, R.raw.qinghuaci);
        try{
            mediaPlayer.stop();
            mediaPlayer.prepare();
            isPlaying = true;
        }catch (Exception e){
            e.printStackTrace();
            isPlaying = false;
        }
        if(isPlaying){
            if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}